import tkinter as tk
from tkinter.font import Font
import main
import cfg


class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("找内鬼")
        self.geometry("800x600")

        # 创建并布局各个组件
        self.create_widgets()

    def create_widgets(self):
        # 获取默认字体的高度
        default_font = Font(font="TkDefaultFont")
        font_height = default_font.metrics("linespace")

        # 设置Label和Entry/Text的高度比例
        label_height = 1
        entry_height = 2 * font_height
        result_text_height = 8 * font_height
        title = ["总承伤人员", "分组人员（贴出来的）", "弩箭+承伤人员", "筛选结果"]

        # 全部人员标题
        all_people_label = tk.Label(self, text=title[0], height=label_height, bg="gray")  # 设置背景色为灰色
        all_people_label.pack(fill=tk.X)

        # 全部人员输入框和滚动条
        self.all_people_frame = tk.Frame(self)
        self.all_people_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 5))
        self.all_people_entry = tk.Text(self.all_people_frame, height=entry_height // font_height, wrap=tk.WORD)
        self.all_people_entry.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        self.all_people_scroll = tk.Scrollbar(self.all_people_frame, command=self.all_people_entry.yview)
        self.all_people_entry.configure(yscrollcommand=self.all_people_scroll.set)
        self.all_people_scroll.pack(side=tk.RIGHT, fill=tk.Y)

        # 被安排人员标题
        scheduled_people_label = tk.Label(self, text=title[1], height=label_height, bg="gray")  # 设置背景色为灰色
        scheduled_people_label.pack(fill=tk.X)

        # 被安排人员输入框和滚动条
        self.scheduled_people_frame = tk.Frame(self)
        self.scheduled_people_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 5))
        self.scheduled_people_entry = tk.Text(self.scheduled_people_frame, height=entry_height // font_height,
                                              wrap=tk.WORD)
        self.scheduled_people_entry.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        self.scheduled_people_scroll = tk.Scrollbar(self.scheduled_people_frame,
                                                    command=self.scheduled_people_entry.yview)
        self.scheduled_people_entry.configure(yscrollcommand=self.scheduled_people_scroll.set)
        self.scheduled_people_scroll.pack(side=tk.RIGHT, fill=tk.Y)

        # 实际参与人员标题
        actual_participants_label = tk.Label(self, text=title[2], height=label_height, bg="gray")  # 设置背景色为灰色
        actual_participants_label.pack(fill=tk.X)

        # 实际参与人员输入框和滚动条
        self.actual_participants_frame = tk.Frame(self)
        self.actual_participants_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 5))
        self.actual_participants_entry = tk.Text(self.actual_participants_frame, height=entry_height // font_height,
                                                 wrap=tk.WORD)
        self.actual_participants_entry.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        self.actual_participants_scroll = tk.Scrollbar(self.actual_participants_frame,
                                                       command=self.actual_participants_entry.yview)
        self.actual_participants_entry.configure(yscrollcommand=self.actual_participants_scroll.set)
        self.actual_participants_scroll.pack(side=tk.RIGHT, fill=tk.Y)

        # 按钮
        bold_font = Font(family="TkDefaultFont", weight="bold")  # 创建加粗字体
        self.find_button = tk.Button(self, text=">>>>>> 点击查询 <<<<<<", command=self.find_not_participated, font=bold_font)  # 使用加粗字体
        self.find_button.pack(fill=tk.X, expand=False, pady=(0, 5))

        # 结果显示标题
        result_label = tk.Label(self, text=title[3], height=label_height, bg="gray")  # 设置背景色为灰色
        result_label.pack(fill=tk.X)

        # 结果显示文本框和滚动条
        self.result_text_frame = tk.Frame(self)
        self.result_text_frame.pack(fill=tk.BOTH, expand=True, ipady=5)
        self.result_text = tk.Text(self.result_text_frame, height=result_text_height // font_height, wrap=tk.WORD)
        self.result_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        self.result_text_scroll = tk.Scrollbar(self.result_text_frame, command=self.result_text.yview)
        self.result_text.configure(yscrollcommand=self.result_text_scroll.set)
        self.result_text_scroll.pack(side=tk.RIGHT, fill=tk.Y)

        # 初始化时读取配置信息并显示在输入框中
        self.cfg_instance = cfg.CCfg()
        self.cfg_instance.SetPrintFunc(lambda s, end: self.show_result(s, end))
        self.load_config_data()

    def load_config_data(self):
        # 假设cfg.CCfg()已经定义
        self.all_people_entry.insert(tk.END, self.cfg_instance.GetAllPeople())
        self.scheduled_people_entry.insert(tk.END, self.cfg_instance.GetScheduledPeople())
        self.actual_participants_entry.insert(tk.END, self.cfg_instance.GetActualParticipants())
        pass

    def find_not_participated(self):
        # 假设main.find_not_participated和cfg.CCfg()已经定义
        self.clear_result()

        all_people = self.all_people_entry.get("1.0", tk.END)
        scheduled_people = self.scheduled_people_entry.get("1.0", tk.END)
        actual_participants = self.actual_participants_entry.get("1.0", tk.END)

        self.cfg_instance.SetAllPeople(all_people)
        self.cfg_instance.SetScheduledPeople(scheduled_people)
        self.cfg_instance.SetActualParticipants(actual_participants)
        main.find_not_participated(self.cfg_instance)
        pass

    def clear_result(self):
        self.result_text.delete("1.0", tk.END)  # 先清空原有内容

    def show_result(self, input_str, end=""):
        """
        该函数用于在self.result_text组件已有内容的末尾添加输入的字符串。

        参数:
        input_str (str): 要添加的字符串内容。
        """
        self.result_text.insert(tk.END, input_str)
        self.result_text.insert(tk.END, end)


if __name__ == "__main__":
    app = App()
    app.mainloop()